<?php

namespace app\common;

use think\Exception;

class Common
{
    /**
     * 后台管理员操作记录
     *
     * @param $request
     * @param string $logInfo
     */
    public static function adminLog($request, $logInfo){
        $data['admin_id'] = session('ADMIN')['id'];
        $data['log_info'] = $logInfo;
        $data['log_ip'] = get_IP();
        $data['log_url'] = $request->path();
        $data['log_time'] = time();

        $model = db('adminlog');
        try {
            $model->insert($data);
        } catch (Exception $e) {
            return ;
        }
    }
    public static function array_group_by($arr, $key)
    {
        $grouped = [];
        foreach ($arr as $value) {
            $grouped[$value[$key]][] = $value;
        }
        if (func_num_args() > 2) {
            $args = func_get_args();
            foreach ($grouped as $key => $value) {
                $parms = array_merge([$value], array_slice($args, 2, func_num_args()));
                $grouped[$key] = call_user_func_array('array_group_by', $parms);
            }
        }
        return $grouped;
    }

    //格式化剩余时间
    public static function time2second($seconds){
        $seconds = (int)$seconds;
        if( $seconds<86400 ){//如果不到一天
            $format_time = gmstrftime('%H时%M分%S秒', $seconds);
        }else{
            $time = explode(' ', gmstrftime('%j %H %M %S', $seconds));//Array ( [0] => 04 [1] => 14 [2] => 14 [3] => 35 )
            $format_time = ($time[0]-1).'天'.$time[1].'时'.$time[2].'分'.$time[3].'秒';
        }
        return $format_time;
    }

}